    package treeHoleDAO;

    import treeHoleSERVICE.OrderByDate;
    import treeHoleSERVICE.User;

    import java.io.*;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    /**
     * @program:IntelliJ IDEA
     * @Description:publicTreeHole实现类
     * @author:Mr.CheCheHuiBujianO
     * @data: 2022/6/19 17:42
     */
    public class PublicTreeHoleIMPH implements PublicTreeHole{
        class MyObjectOutputStream extends ObjectOutputStream{
            public MyObjectOutputStream(OutputStream out) throws IOException {
                super(out);
            }
            protected MyObjectOutputStream() throws IOException, SecurityException {
                super.reset();
            }

            @Override
            protected void writeStreamHeader() throws IOException {
                return;
            }
        }
        class MyObjectInputStream extends ObjectInputStream{
            public MyObjectInputStream(InputStream in) throws IOException {
                super(in);
            }
            @Override
            protected void readStreamHeader() throws IOException, StreamCorruptedException {
            }
        }
        /**
         * @Description: 创建一个文本文件作为公用树洞（仅能被创建一次）
         * @Param: []
         * @return:[]
         * @Author: Mr.Lin
         * @Date: 2022/6/16 17:39
         */
        @Override
        public void treeHoleFile() throws IOException {
            File f = new File("src/main/java/treeHoleDAO/treeHole.txt");
            if (!f.exists()){
                f.createNewFile();
            }
        }

        /**
         * @param publicHistoryPostMessage
         * @Description: 按照时间的先后将我们用户内容进行排序
         * @Param: [publicHistoryPostMessage]
         * @return:java.util.List<treeHoleSERVICE.User.UserHistory>
         * @Author: Mr.Lin
         * @Date: 2022/6/16 23:30
         */
        @Override
        public List<User.UserHistory> treeHolePersonalPostMessageSort(List<User.UserHistory> publicHistoryPostMessage) {
            Collections.sort(publicHistoryPostMessage,new OrderByDate());
            return publicHistoryPostMessage;
        }

        /**
         * @param alreadySortPostMessage
         * @Description: 将我们排好序的帖子进行写入我们树洞公共文本文件中
         * @Param: [alreadySortPostMessage]
         * @return:void
         * @Author: Mr.Lin
         * @Date: 2022/6/16 23:31
         */
        @Override
        public void writeTreeHoleFile(List<User.UserHistory> alreadySortPostMessage) {
            MyObjectOutputStream oos = null;
            try{
                oos = new MyObjectOutputStream(new FileOutputStream("src/main/java/treeHoleDAO/treeHole.txt",true));
                for(int i=0;i<alreadySortPostMessage.size();i++){
                    oos.writeObject(alreadySortPostMessage.get(i));
                }
                oos.flush();
            }catch(Exception e) {
                e.printStackTrace();
            }finally{
                try{
                    if(oos != null){
                        oos.close();
                    }
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        /**
         * @Description: 将我们公用树洞文本文件的内容进行一个读出的操作，将其展示在我们树洞页面之中 将我们数据返回出来便于删除操作
         * @Param: []
         * @return:java.util.List<treeHoleSERVICE.User.UserHistory>
         * @Author: Mr.Lin
         * @Date: 2022/6/16 23:20
         */
        @Override
        public List<User.UserHistory> readTreeHoleFileAndShowUI() {
            MyObjectInputStream ois = null;
            List<User.UserHistory> zanCun = new ArrayList<>();
            try {
                ois = new MyObjectInputStream(new FileInputStream("src/main/java/treeHoleDAO/treeHole.txt"));
                Object h = null;
                while ((h =  ois.readObject()) != null) {
                    zanCun.add((User.UserHistory) h);
                }
            } catch (Exception e) {

            } finally {
                try {
                    if (ois != null) {
                        ois.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return zanCun;
        }

        /**
         * @param title
         * @Description: 根据我们题目内容进行一个删除，记得删除要将相应文本内容一起删除，记得再次调用展示函数和写入函数
         * @Param: [title]
         * @return:void
         * @Author: Mr.Lin
         * @Date: 2022/6/16 20:17
         */
        @Override
        public void deleteTreeHoleContent(String title) {
            List<User.UserHistory> test=this.readTreeHoleFileAndShowUI();
            int z=test.size();
            for(int i=0;i<z;i++){
                if(test.get(i).title==title){
                    test.remove(test.get(i));
                }
            }
            this.writeTreeHoleFile(test);
        }

        /**
         * @param
         * @Description: 返回帖子总数
         * @Param: [title]
         * @return:void
         * @Author: Mr.Zhang
         * @Date: 2022/6/16 20:17
         */
        @Override
        public int Sum() {
            return this.readTreeHoleFileAndShowUI().size();
        }
    }
